<template>
  <div>
    <a-card :bordered="false">
      <f-search-form :more="false" ref='searchs' @searchHandler="searchHandler">
        <f-search-form-item label="单位名称" name="invoiceunitName"/>
        <f-search-form-item label="联系人" name="linkMan" slot="advanced"/>
        <f-search-form-item label="法人" name="legalPerson" slot="advanced"/>
      </f-search-form>
      <f-table ref="table" rowKey="invoiceUnitId" :columns="columns"  :scroll="{x:true}"  :multiple="true" :operator="operator" :params="queryParam" :api="api.invoiceunit">
         <span slot="invoiceunitState" slot-scope="text, record, index">
          <a-switch v-model="record.invoiceunitState==1" checkedChildren="已启用" unCheckedChildren="已禁用" @change="(checked) => setState(checked,record)"/>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="$refs.drawer.showDrawer(record)">编辑</a>
            <a-divider type="vertical"/>
          </template>
           <template>
            <a @click="$refs.drawer.showDrawer(record)">设置月额度</a>
            <a-divider type="vertical"/>
          </template>
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
            <a-menu slot="overlay">
              <a-menu-item v-if="$auth('table.delete')">
                <a href="javascript:;" @click="$refs.table.del(record)">删除</a>
              </a-menu-item>
            <a-menu-item >
                 <a href="javascript:;" @click="$refs.queue.handleShow(record)">查看月度明细</a>
             </a-menu-item>

            </a-menu>
            </a-dropdown>
        </span>
      </f-table>
      <invoiceunit-drawer ref="drawer"></invoiceunit-drawer>
      <queue-drawer ref='queue'></queue-drawer>
    </a-card>

    <a-modal v-model="dialogFormVisible" title="月额度" on-ok="onok" :maskClosable='false'>
      <template slot="footer">
        <a-button key="back" @click="onclose">
          取消
        </a-button>
        <a-button key="submit" type="primary" @click="onok">
          提交
        </a-button>
      </template>

      <p>
        <a-input   v-model='monthQuota'  placeholder="请输入当月额度"   ></a-input>
      </p>
    </a-modal>
  </div>
</template>
<script>
  import invoiceunitDrawer from './invoiceunit_drawer.vue'
  import queueDrawer from './quota'
  import api from './js/api'

  export default {
    name: 'TableList',
    components: {
      invoiceunitDrawer,
      queueDrawer
    },
    data() {
      let me = this
      //操作按钮
      let operator = []
      operator.push({
        name: '添加',
        icon: 'plus',
        that: this,
        handler: function(table, that) {
          that.$refs.drawer.showDrawer()
        }
      })
      operator.push({
        name: '删除',
        icon: 'delete',
        batch: true,
        handler: function(table) {
          table.remove()
        }
      })
      operator.push({
        name: '设置月额度',
        icon: 'file-protect',
        batch: true,
        that: this,
        handler: function(table, that) {
          that.batchBtns(table)
        }
      })
      return {
        dialogFormVisible: false,
        invoiceUnitIds: '',
        monthQuota: 0,
        api: Object.assign(api, this.api),
        //列表初始化查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '#',
            width: '50px',
            align: 'center',
            customRender: (text, record, index) => index + 1
          },
          {
            title: '单位名称',
            align: 'left',
            width: '250px',
            ellipsis: true,
            dataIndex: 'invoiceunitName'
          },
          {
            title: '开户行',
            align: 'left',
            ellipsis: true,
            dataIndex: 'bankName'
          },
          {
            title: '卡号',
            align: 'left',
            ellipsis: true,
            dataIndex: 'bankNo'
          },
          {
            title: '税率',
            align: 'center',
            width: '80px',
            dataIndex: 'taxNo'
          },
          {
            title: '联系人',
            align: 'center',
            ellipsis: true,
            dataIndex: 'linkMan'
          },
          {
            title: '签约日期',
            align: 'center',
            ellipsis: true,
            dataIndex: 'contractDate'
          },
          {
            title: '状态',
            align: 'center',
            dataIndex: 'invoiceunitState',
            scopedSlots: { customRender: 'invoiceunitState' }
          },
          {
            title: '额度开始日期',
            align: 'center',
            dataIndex: 'cycleStart'
          },
          {
            title: '总额度',
            align: 'center',
            ellipsis: true,
            dataIndex: 'totalQuota'
          },
          {
            title: '已用额度',
            align: 'center',
            ellipsis: true,
            dataIndex: 'useQuota'
          },
          {
            title: '剩余额度',
            align: 'center',
            ellipsis: true,
            dataIndex: 'residueQuota'
          },
          {
            title: '当年额度',
            align: 'center',
            ellipsis: true,
            dataIndex: 'yearQuota'
          },
          {
            title: '月额度',
            align: 'center',
            ellipsis: true,
            dataIndex: 'monthQuota'
          },
          {
            title: '浮动额度',
            align: 'center',
            ellipsis: true,
            dataIndex: 'slidingQuota'
          },
          {
            title: '操作',
            align: 'center',
            scopedSlots: { customRender: 'action' },
            fixed: 'right'

          }
        ],
        operator: operator
      }
    },
    created() {
    },
    methods: {
      batchBtns(table) {
        let that = this
        let chooseRows = [...table.selectedRowKeys]
        // console.log("##########", data)
        console.log('##########11111', chooseRows)
        if (chooseRows.length > 0) {
          let idArray = new Array()
          chooseRows.map(item => {
            idArray.push(item)
          })
          that.batchCreateVoucher(idArray.join(','))
        } else {
          that.$message.warning('请选择操作单位')
          return false
        }
      },
      batchCreateVoucher(ids, isMerge) {
        this.reset()
        this.invoiceUnitIds = ids
        this.dialogFormVisible = true
      },
      onclose() {
        this.dialogFormVisible = false
        this.reset()
      },
      onok() {
        this.http.confirm(this, {
          content: '设置单位月额度为' + this.monthQuota + '元吗?',
          url: this.api.invoiceunit.setMonthQuota,
          requestBody: false,
          data: {
            ids: this.invoiceUnitIds,
            monthQuota: this.monthQuota
          },
          success: function(me, data) {
            me.$refs.searchs.searchHandler()
            me.onclose()
          }
        })
      },
      reset() {
        this.monthQuota = 0
        this.invoiceUnitIds = ''
      },
      setState(check, record) {
        this.http.post(this, {
          url: this.api.invoiceunit.setState,
          data: {
            ids: record.invoiceUnitId,
            invoiceunitState: Number(check)
          },
          success(me, data) {
            record.invoiceunitState = data
          }
        })
      }
    }
  }
</script>
